package com.jlect.swebing.ui.client;

import java.util.HashMap;
import java.util.Map;

/**
 * Immutable color implementation. It stores unique instances in static hashmap
 *
 * @author Sergey Kozmin
 * @since 10.11.2007 22:46:02
 */
public class Color {
    private static Map colors = new HashMap();

    public final static Color WHITE         = getInstance(255, 255, 255);
    public final static Color LIGHT_GRAY    = getInstance(192, 192, 192);
    public final static Color GRAY          = getInstance(128, 128, 128);
    public final static Color DARK_GRAY     = getInstance(64, 64, 64);
    public final static Color BLACK 	    = getInstance(0, 0, 0);
    public final static Color RED           = getInstance(255, 0, 0);
    public final static Color PINK          = getInstance(255, 175, 175);
    public final static Color ORANGE 	    = getInstance(255, 200, 0);
    public final static Color YELLOW 	    = getInstance(255, 255, 0);
    public final static Color GREEN 	    = getInstance(0, 255, 0);
    public final static Color MAGENTA	    = getInstance(255, 0, 255);
    public final static Color CYAN 	        = getInstance(0, 255, 255);
    public final static Color BLUE 	        = getInstance(0, 0, 255);

    private int color;

    public static Color getInstance(int r, int g, int b) {
        Integer color = ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF);
        if (colors.containsKey(color)) {
            return (Color) colors.get(color);
        } else {
            Color c = new Color(color);
            colors.put(color, c);
            return c;
        }
    }

    protected Color(int rgb) {
        color = rgb;
    }

    public int getRGB() {
        return color;
    }

    public int getRed() {
        return (getRGB() >> 16) & 0xFF;
    }

    public int getGreen() {
        return (getRGB() >> 8) & 0xFF;
    }

    public int getBlue() {
        return getRGB() & 0xFF;
    }

    public String toString() {
        return "Color{" +
                "color=" + color +
                '}';
    }
}
